const jwt = require('jsonwebtoken')

const SECRET_KEY = 'wh220516'

function getToken(userId, expiresIn = '7d') {
    const token = jwt.sign({
        userId
    }, SECRET_KEY, {
        expiresIn
    })
    return token
}

exports.getToken = getToken


function verifyToken(token) {
    return new Promise((resolve, reject) => {
        jwt.verify(token, SECRET_KEY, (error, decode) => {
            if (error) {
                reject("token过期或非法")
            } else {
                resolve(decode.userId)
            }
        })
    })
}
exports.verifyToken = verifyToken